package org.dftproject.genesis.ui.pages.pedigree.icon;

import org.dftproject.genesis.data.genealogy.Sex;
import org.dftproject.genesis.ui.figures.dualtree.DualTreeFigure;
import org.dftproject.genesis.ui.figures.dualtree.ImprovedWalkersLayout;
import org.dftproject.genesis.ui.pages.pedigree.tree.OneToManyConnection;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;

public class PedigreeIconFigure extends Figure {

	private final DualTreeFigure dualTree;
	
	public PedigreeIconFigure() {
		setLayoutManager(new CenterStackLayout());
		
		dualTree = new DualTreeFigure() {
			
			@Override
			protected OneToManyConnection createOneToManyConnection() {
				return new OneToManyConnection(1);
			}
			
		};
		dualTree.setLayoutManager(new ImprovedWalkersLayout(7, 3));
		add(dualTree);
		
		NodeFigure root = new NodeFigure(Sex.Male);
		dualTree.setRoot(root);
		addParents(root, 2);
	}
	
	protected NodeFigure addLeft(IFigure child) {
		NodeFigure node = new NodeFigure(Sex.Male);
		dualTree.addAncestor(child, node, 0);
		return node;
	}
	
	protected NodeFigure addRight(IFigure child) {
		NodeFigure node = new NodeFigure(Sex.Female);
		dualTree.addAncestor(child, node, 1);
		return node;
	}

	protected void addParents(NodeFigure child, int generations) {
		NodeFigure left = addLeft(child);
		if (generations > 1)
			addParents(left, generations - 1);
		
		NodeFigure right = addRight(child);
		if (generations > 1)
			addParents(right, generations - 1);
	}
	
}
